Obama '08
New-formula-starburst

autotest is now autospec - How to Set Up Autospec for RSpec and Rails with ZenTest

2

Automatically running your specs as you write code is a very effective way to improve your Behavior Driven Developent (BDD) practices and overall productivity. When I’m building new features in my Rails apps, I keep one terminal window in view with autospec running continuously, detecting changes to my files, and automatically running all relevant specs that the file may affect. It gives me nearly instant feedback about any change that I made that has completed a pending feature, or may have broken something inadvertently.

If you’re struggling with sticking with BDD, try setting up autospec and spend a few hours trying my method:

I’m assuming that you already have RSpec up and running and know how to write specs (as of writing this, I’m using RSpec 1.1.4). If not, here’s a good introduction to Rspec.

Install required gems

sudo gem install ZenTest

Create a .autotest file

In your home directory create a file named .autotest and paste the following:

Autotest.add_hook :initialize do |at|
  %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}
end

This will tell autotest to ignore the SVN and Git hidden files within your code.

Write specs. Make it green. Repeat.

Now you’re ready to code furiously! Launch autospec in a new terminal window from your Rails project root. At first, autospec will run through all your specs just like rake spec, but will then wait. As soon as it detects changes to a file in your application, autospec will figure out what specs to run and automatically execute them.

Try this best practice to fully realize the benefits and pleasure of BDD:

1. With autospec running, write a few specs that describe a feature or requirement that you want to implement. For a simple example, let’s assume you have just generated a Rating model. The requirement is that it has a score between 1 and 10. Open up spec/models/rating_spec.rb and …

describe Rating do

  before(:each) do
    @rating = Rating.new
  end

  it "should not have a score less than 1" do
    @rating.score = 0
    @rating.should have_exactly(1).error_on(:score)
  end

  it "should not have a score greater than 10" do
    @rating.score = 11
    @rating.should have_exactly(1).error_on(:score)
  end

  (1..10).each do |i|
    it "should be valid with a score of #{i}" do
      @rating.score = i
      @rating.should be_valid
    end
  end
end

2. Now as soon as you save this file in rating_spec.rb, autospec will be painted red with failures. Let’s implement this requirement in app/models/rating.rb:

class Rating < ActiveRecord::Base
  validates_inclusion_of :score, :in => 1..10
end

3. Save the file, and glance over to your autospec screen which should now be green. Congratulations, you just succeeded at behavior driven development! Now repeat this process by writing a few specs for a single feature or requirement first, then switch over to your actual models or controllers and make them pass. Keep it green, keep it clean.

Notes about autospec

  1. Autospec won’t pick up changes to your database that you make via migrations. If you change the database structure at all, exit autospec by hitting CTRL-C twice, then run rake spec to reload your test database. Then you can start autospec again.
  2. Autospec also won’t pick up new spec files while it is running. Just exit and start autospec again.
  3. After a spec file with failures has gone all-green, autospec will re-run the entire spec suite as a regression.
  4. There are ways that people have hooked up autospec to Growl notifications. I think this is lame and gimmicky. You’re a geek. Use the terminal.

Samsung Instinct tethered to Mac OS X as a Sprint 3G wireless modem

24

After a month or so of frustration, anger and determination, I’ve finally hacked my Sprint/Samsung Instinct to work as a modem. Inside my San Francisco apartment, using the USB cable that comes with the phone, I have connected my Macbook to Sprint’s 3G wireless network and clocked speeds at 1100 kb/s down and about 300 kb/s up. This is with about 4-bars of service … I’m going to try it tomorrow in the park where I should get a full signal.

UPDATE SEPT 3 from Dolores Park, San Francisco – I’m in the park now, will a full 6 bars of service running on all battery. Download speed blew me away at 1529 kb/s according to Speedtest.net. That’s faster than some wired DSL speeds!

The Instinct is Sprint’s first real answer to the iPhone, and the first consumer phone to be capable of Sprint’s fastest “Rev A” 3G wireless data network. The Instinct is also capable of “Phone as a Modem (PAM)” service, according to some of Samsung’s literature. I had resisted getting the iPhone, for various reasons, and Sprint finally had a good deal on the Instinct with a pretty decent feature set. I paid about $80 after rebates for the phone, and my monthly costs are about 40% less than what I’d pay if I switched to AT&T.

The phone is fast, responsive, and overall very good. The interface isn’t nearly as slick or polished as the iPhone, but the Instinct mostly gets the job done … and so far I’m pleased to see some software updates getting pushed down. The Phone as Modem thing, though, was a huge disappointment …

Even though the Instinct is capable of PAM, Sprint, has so far decided to not support it. The Instinct requires Sprint’s “Simply Everything” plan, which includes unlimited 3G internet, but the fine print says that phone as modem is not included. Even worse … Sprint sells users a $15 per month plan add-on for PAM capability for use with some of the other devices like the Blackberry and Mogul … but they don’t offer it for the Instinct or any of it’s required plans.

I confirmed these facts today when I called Sprint Customer Support, and asked them about PAM support for my Instinct. Basically, even though the phone is perfectly capable, Sprint has decided to block Instinct owners from using PAM. I am perfectly willing to pay … I think $15 per month is reasonable for the convenience of occasional PAM use … but Sprint won’t let me. Time to start hacking …

Keep reading for the How-To

jQuery and Rails trick - Multiple submit buttons for a single form

1

Say you have a HTML form with two buttons that you each want to submit to different RESTful actions. For example, an email form with a ‘Send’ and ‘Delete’ button, which should each POST to different actions.

With some intelligent conventions, and a few lines of jQuery, we can accomplish that nicely. Here’s a simplified example.

the RHTML View

<% form_for @message, :url => '', :html => { :id => 'message' } do |f| -%>
<%= f.text_field 'subject' %>
<%= f.text_field 'body' %>
<% end -%>
<button type="button" name="send">Send</button>
<button type="button" name="save">Save as Draft</button>

jQuery

  $(document).ready(function(){
    $("button").click(function() {
      $("form#message").attr('action', '/message/' + $(this).attr('name'));
      $("form#message").submit();
    });
  });

This will enable all the buttons on the page so that when clicked, the button will submit to /message/name where name is the name attribute on the button tag.

Inept Recruiters

2

Attention all you recruiters / “staffing specialists” / spammers out there. Here’s an amusing story about some turd named Pradipta Archiputra aka “Max Archie” that should illustrate to you how NOT to search for skilled talent in the Ruby world.

Some time last night I received a brief email from said recruiter. Here’s what it looked like:

I have a couple of Ruby on Rails position, wanted to know if you are interested?

Max Archie
Technical Recruiter
Prodigus Source
Cell: 219-669-9216
Phone: 312-235-2365
Max@prodigussource.com
Note: I intentionally made no effort to disguise this guy’s identity or contact info. You’ll see why in a minute.

At first glance, aside from the bad grammar and lack of any real details about the positions available, I almost ignored this one, as I get this type of unsolicited message from some recruiter at least every week. What I quickly noticed was that this message was sent to 400 other people in the To: field of the email. FAIL.

What happened next was truly amazing. First, my Twitter started blowing up with rants from fellow RoR coders who received the same email and noticed the failure of epic proportions in the way the message was sent. Not more than a few minutes later, the Reply All train started … and somehow Pradipshit sparked a worldwide impromptu discussion of 400-odd random technologists.

Less than 20 hours and 89 replies later, a true phenomenon has arisen. A Google Group has been created dubbed Pradipta’s Rolodex for all of us chosen ones to continue ranting and stay in touch. One creative member of Pradipta’s Kids even made a nice logo taken from Prodigus cheesy stock photo on their website. Now we’ve even got a website called The Pradipta 416 and a sexy badge to acknowledge our inclusion:

The Few, The Proud, The Pradipta 416

All I can say is, wow. I have met many recruiters in my days as a technologist … but this guy takes the cake as the sleaziest. Please, if you’re in the recruiting or staffing industry, and insist on spamming to source your talent, at least learn how to use the BCC field or get a proper email list manager.

That’s my rant for the day. Now back to work.

Create an Encrypted Partition on Mac OS X using Truecrypt

7

My new Macbook Pro just arrived and I’m spending a good part of the day setting her up. As I write this, I’m in the process of using Truecrypt, which is only recently available for Mac OS X, to create an encrypted partition on my mac hard drive. I’ve done this before on my Ubuntu laptop, and it has worked great. What follows is a summary of my experience with Truecrypt on the Mac and a step-by-step for you to try at home.

An encrypted partition provides you with a high amount of security, with very little inconvenience or impact on performance. Truecrypt has two ways of creating encrypted storage, a file container, or a whole partition. An encrypted file container is nice because it’s easy to move around … i.e. put on a SD card or thumb drive … for small amounts of sensitive data. For my purpose, I want to encrypt ALL my personal stuff like documents, code, tax returns and records, photos, etc., so an encrypted partition is the way to go.

Encrypting a partition is more of a seamless integration with your Macbook, too. I’ve also read reports that it’s faster to access encrypted files from a partition than file containers. Basically, the encrypted partition looks like a second hard drive in finder, once you mount it and provide your Truecrypt passphrase on bootup. It looks and acts just like any drive or folder, you can store files, run applications, create links, etc … just like any drive. Truecrypt encrypts and decrypts everything on the fly, with virtually unnoticeable overhead. If your laptop ever turns up in the hands of a bad guy or rogue customs agent, they wouldn’t be able to access any of your data

Sounds good? Read on to find out how …

How-To Update Ubuntu Servers to Close Ruby Vulnerabilities

5

It was announced the other day that some arbitrary code execution vulnerabilities were discovered in almost all production versions of Ruby out in the wild. I’m not sure how vulnerable your typical Ruby on Rails application servers would be, but I’m taking no chances. I run this blog, and all my other production sites on Ubuntu. Updating to the latest patched version of ruby was easy:

$ sudo apt-get install build-essential libssl-dev libreadline5-dev zlib1g-dev
$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p22.tar.gz
$ tar zxvf ruby-1.8.7-p22.tar.gz
$ cd ruby-1.8.7-p22
$ ./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr --with-zlib-dir=/usr
$ make
$ sudo make install

And you’re done. The only sorta tricky part there is the ./configure command, which requires those options to tell the compiler to enable Readline and OpenSSL support which are most often needed in a Ruby on Rails environment. To check and make sure it’s working, type these commands and verify that the output looks like this:

$ which ruby
/usr/local/bin/ruby
$ ruby --version
ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-linux]
$ ruby -ropenssl -rzlib -rreadline -e "puts :success"
success

Now, run your tests, restart mongrels, and you’re safe. Phew.

Older posts: 1 2 3 4